﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.Globalization;
using System.IO;

namespace QUANLYDIEMHOCSINH
{
    public partial class ThemHS : Form
    {
        public ThemHS()
        {
            InitializeComponent();
            set_AN();
            LoadDanhSachlop();
            picanh.Image = Image.FromFile("hinh\\no_picture.PNG");
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDanhSachlop()
        {
            LOPBUS lopbus = new LOPBUS();
            cblop.DataSource = lopbus.layDSL();
            cblop.DisplayMember = "TENLOP";
            cblop.ValueMember = "MALOP";
            cblop.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            HOCSINHBUS hs = new HOCSINHBUS();
            HOCSINHDTO hocsinh = new HOCSINHDTO();
            hocsinh.TENHS = txthoten.Text;
            DateTime dt = DateTime.Parse(dtngaysinh.Text, new CultureInfo("vi-VN", true));
            hocsinh.NGAYSINH = dt; 
            hocsinh.DIACHI = txtdiachi.Text;
            hocsinh.SODIENTHOAIPHUHUYNH = txtsdt.Text;
            if (rdnam.Checked == true)
            {
                hocsinh.GIOITINH = 1;
            }
            else
                hocsinh.GIOITINH = 0;
            hocsinh.TINHTRANG = 1;
            if (ofdHinh.FileName != "")
            {
                picanh.Image.Save(Application.StartupPath + "\\hinh\\" + System.IO.Path.GetFileName(ofdHinh.FileName));
                hocsinh.ANH = "hinh\\" + System.IO.Path.GetFileName(ofdHinh.FileName);
            }
            else
            {
                hocsinh.ANH = "";
            }
            int malop=int.Parse(cblop.SelectedValue.ToString());
            if (hs.them_hs(hocsinh, malop) == 1)
                MessageBox.Show("Thêm Thành Công!");
           else if(hs.them_hs(hocsinh, malop) == 2)
                MessageBox.Show("Học Sinh Đã Tồn Tại!");
            else
                MessageBox.Show("Thêm Thất Bại!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHinh_Click(object sender, EventArgs e)
        {
            ofdHinh.Filter = "file jpg(*.jpg)|*jpg";
            DialogResult dlgResual = ofdHinh.ShowDialog();
            if (dlgResual == DialogResult.OK)
            {
                picanh.ImageLocation = ofdHinh.FileName;
            }
        }
    }
}
